import { _decorator, assert, AudioClip, AudioSource, Component, Node } from 'cc';
import { Check } from '../hyxh/core/Check';
const { ccclass, property } = _decorator;

@ccclass("BKAudioController")
export class BKAudioController extends Component { 

    @property(AudioSource)
    public _audioSource: AudioSource = null!;

    @property(AudioClip)
    public clip: AudioClip = null!;   

    onLoad () {
        // 获取 AudioSource 组件
        const audioSource = this.node.getComponent(AudioSource)!;
        // 检查是否含有 AudioSource，如果没有，则输出错误消息
        Check.defined("audioSource", audioSource);
        // 将组件赋到全局变量 _audioSource 中
        this._audioSource = audioSource;
    }

    play () {
        // 播放音乐
        this._audioSource.play();
    }

    pause () {
        // 暂停音乐
        this._audioSource.pause();
    }

    playOneShot () {
        this._audioSource.playOneShot(this.clip, 1);
    }
}